﻿using System;
using System.Collections.Generic;
using System.Xml.Linq;

namespace APIBetaSeries.Models.Status
{
    public class Status : ObjetBase
    {
        public WebSiteStatus WebSiteStatus { get; set; }
        public ApiStatus ApiStatus { get; set; }

        public Status(XDocument value)
            : base(value)
        {
            if (value != null)
            {
                XElement root = value.Element("root");
                if (root != null)
                {
                    foreach (XElement element in root.Elements())
                    {
                        if (element.Name == "website")
                        {
                            XElement xStatus = element.Element("status");
                            XElement xDatabase = element.Element("database");
                            if (xStatus != null && xDatabase != null)
                            {
                                WebSiteStatus = new WebSiteStatus { Status = xStatus.Value, Database = xDatabase.Value };
                            }
                        }
                        if (element.Name == "api")
                        {
                            XElement xVersion = element.Element("version");
                            if (xVersion != null)
                                ApiStatus = new ApiStatus { Version = xVersion.Value };
                            XElement xVersions = element.Element("versions");
                            ApiStatus.VersionStatusList = new List<VersionStatus>();

                            if (xVersions != null)
                            {
                                IEnumerable<XElement> xVersionVersion = xVersions.Elements("version");
                                foreach (XElement xElement in xVersionVersion)
                                {
                                    VersionStatus versionStatus = new VersionStatus();
                                    XElement xVersionVersionDate = xElement.Element("date");
                                    if (xVersionVersionDate != null)
                                    {
                                        String formattedDate = String.Concat(xVersionVersionDate.Value.Substring(0, 4), "/", xVersionVersionDate.Value.Substring(4, 2), "/", xVersionVersionDate.Value.Substring(6, 2));
                                        versionStatus.Date = Convert.ToDateTime(formattedDate);
                                    }
                                    XElement xVersionVersionChanges = xElement.Element("changes");
                                    if (xVersionVersionChanges != null)
                                    {
                                        IEnumerable<XElement> xVersionVersionChangesChange = xVersionVersionChanges.Elements("change");
                                        versionStatus.ChangeStatusList = new List<ChangeStatus>();
                                        foreach (XElement xVersionVersionChangesChangeValue in xVersionVersionChangesChange)
                                        {
                                            XElement xType = xVersionVersionChangesChangeValue.Element("type");
                                            XElement xValue = xVersionVersionChangesChangeValue.Element("value");
                                            if (xType != null && xValue != null)
                                            {
                                                ChangeStatus changeStatus = new ChangeStatus
                                                    {
                                                        Type = xType.Value,
                                                        Value = xValue.Value
                                                    };
                                                versionStatus.ChangeStatusList.Add(changeStatus);
                                            }
                                        }
                                    }
                                    ApiStatus.VersionStatusList.Add(versionStatus);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
